package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import androidx.viewpager2.widget.ViewPager2;
import easier.recycler.BaseViewHolder;
import easier.recycler.InfinitePager;
import easier.recycler.TypeViewHolder;
import java.util.ArrayList;
import java.util.List;

public class MainActivity80 extends AppCompatActivity {

    private ViewPager2 mViewPager2;
    private ViewPager2 mViewPager;
    private Button mUpdate;
    private InfinitePager mInfinitePager;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity80.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main80 );
        initView();
    }

    private void initView() {

        mViewPager2 = (ViewPager2) findViewById( R.id.viewPager );
        mInfinitePager = new InfinitePager( mViewPager2, convert() );
        mInfinitePager.start( 2000 );
        mUpdate = (Button) findViewById( R.id.update );
        mUpdate.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                mInfinitePager.update( convertUpdate() );
            }
        } );
    }

    private List< TypeViewHolder > convert() {

        ArrayList< TypeViewHolder > list = new ArrayList<>();
        for ( int i = 0; i < 5; i++ ) {
            list.add( new BaseViewHolder( R.layout.activity_main63_item ) {
                @Override
                protected void onBind( ViewHolder viewHolder, View root, int position, int type, List< Object > payloads ) {

                    super.onBind( viewHolder, root, position, type, payloads );
                    ((TextView) root.findViewById( R.id.text )).setText( "item" + position );
                }
            } );
        }
        return list;
    }

    private List< TypeViewHolder > convertUpdate() {

        ArrayList< TypeViewHolder > list = new ArrayList<>();
        for ( int i = 0; i < 10; i++ ) {
            list.add( new BaseViewHolder( R.layout.activity_main63_item_1 ) {
                @Override
                protected void onBind( ViewHolder viewHolder, View root, int position, int type, List< Object > payloads ) {

                    super.onBind( viewHolder, root, position, type, payloads );
                    ((TextView) root.findViewById( R.id.text1 )).setText( "新数据:" + position );
                }
            } );
        }
        return list;
    }
}